#include <iostream>
#include "Serveur.h"

using namespace std;

/*
// fonction appelée par le thread qui gere un client.
void * gere_client(void * arg)
{  
  Client client = *((Client*)arg);
  char msg[100];
  int n, encore=1;
  
  while(encore)
    {
      n = lireLigne(client.socket, msg, 100);
      cout<<client.name<<"("<<client.socket<<")"<<" envoie : "<<msg<<"\n";
      if(strcmp(msg, "QUIT")==0) encore =0;
    } // fin de la gestion du client.
  pthread_cancel(*(client.th_cla));
  close(client.socket);

  return NULL;
}



void * gere_client_test(void * arg)
{  
  Client client = *((Client*)arg);
  char msg[100];
  int encore=1;
  ListeChaines *nnn;
  
  cout<<"Gere client test.\n";
  while(encore)
    {
      cout<<nnn<<endl;
      nnn = lireListeChaines(client.socket);
      cout<<nnn<<endl;
      cout<<"je lis\n";
      nnn->affiche();
      //cout<<client.name<<"("<<client.socket<<")"<<" envoie : "<<nn<<"\n";
      if(strcmp(msg, "QUIT")==0) encore =0;
    } // fin de la gestion du client.
  pthread_cancel(*(client.th_cla));
  close(client.socket);

  return NULL;
}


int main(int argc, char * argv[]){
  int port, sock;
  int taille=sizeof(struct sockaddr_un), socketClient;
  struct sockaddr_in cli_addr;
  pthread_t thread_client;
  pthread_mutex_t verrou;
  struct Client client;
  //-------------------------------------initialisation du socket serveur
  port=atoi(argv[1]);
  sock = initSocketServeur(port);
  //-----------------------------------fin des initialisations

  if(sock != -1)
    {
      pthread_mutex_init(&verrou, NULL);
      while (1) { 
	std::cout<<"attente de connexion"<<std::endl;
	socketClient = accept(sock, (struct sockaddr *) &cli_addr,(socklen_t *) &taille);
	struct hostent * info=gethostbyaddr((char*)&cli_addr.sin_addr.s_addr, 4, AF_INET);
	if(info) std::cout<<"un client s'est connecté : "<<info->h_name<<std::endl;
	else std::cout<<"un client s'est connecté"<<std::endl;

	client.socket = socketClient;
	if(info) client.name = info->h_name;
	else client.name = "inconnu";
        client.threadClient = &thread_client;
	pthread_create(&thread_client, NULL, gere_client_test, (void *)&client);
        //pthread_join(thread_client, NULL);
      }
    }
  else std::cout<<"Echec création socket.\n";
}
*/

int main(int argc, char * argv[])
{
    int port = 8888;
    if(argc > 1) port = atoi(argv[1]);
    Serveur *serveur = new Serveur(port);
    serveur->lancerServeur();
}
